<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\Services\OrderService;
use App\Models\Order;
use Illuminate\Http\Request;

class TianzhenlanController extends Controller
{
    /**
     * 预定成功通知
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bookresultnotify(Request $request) {
        $snForCompany   = $request->input('app_order_sn');
        $company_sn     = $request->input('order_sn');
        $resultCode     = $request->input('result_code');
        $resultMsg      = $request->input('result_msg');

        $order  = Order::where('sn_for_company', $snForCompany)->first();
        if ($resultCode == 'SUCCESS') {
            // 预定成功
            if ($order && !$order->company_sn) {
                $order->update(['company_sn'=>$company_sn, 'confirm_at' => datetime(), 'status' => Order::STATUS_CONFIRM_OK]);
                OrderService::notify_sip_book_status($order->id);
            }
        } else {
            // 预定失败
            $order->update(['update_count' => $order->update_count - 1]);

            // 发送短信通知
        }
        return self::success(['app_order_sn' => $snForCompany, 'order_sn' => $company_sn], 0, 'ok');
    }

    /**
     * 修改时间通知
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function booktimemodifynotify(Request $request) {
        $snForCompany   = $request->input('app_order_sn');
        $order_sn       = $request->input('order_sn');
        $bookTime       = $request->input('book_datetime');
        $resultCode     = $request->input('result_code');
        $order          = Order::where('sn_for_company', $snForCompany)->first();
        if ($resultCode == 'SUCCESS') {
            // 修改成功通知
            $order->update(['time' => $bookTime.":00", 'status' => Order::STATUS_CONFIRM_OK, 'confirm_at' => datetime()]);
        } else {
            // 发送短信通知
            $order->update(['update_count' => $order->update_count - 1]);
            // 恢复修改次数
        }

        return self::success(['app_order_sn' => $snForCompany, 'order_sn' => $order_sn], 0, 'ok');
    }

    /**
     * 取消预定成功通知
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelnotify(Request $request) {
        $snForCompany   = $request->input('app_order_sn');
        $order_sn       = $request->input('order_sn');
        $cancelResult   = $request->result_code;
        $order          = Order::where('sn_for_company', $snForCompany)->first();

        // 取消订单后，要重新设置sn
        if ($cancelResult == 'SUCCESS') {
            $order->sn_for_company  = order_sn();
            $order->company_sn      = "";
            $order->save();
            Order::reset($order->id);
        } else {
            // 取消失败 对客服进行提醒

        }

        return self::success(['app_order_sn' => $snForCompany, 'order_sn' => $order_sn], 0, 'ok');
    }


    /**
     * 到店核销通知
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function consumenotify(Request $request) {
        $app_order_sn   = $request->input('app_order_sn');
        $order_sn       = $request->input('order_sn');
        return self::success(['app_order_sn' => $app_order_sn, 'order_sn' => $order_sn], 0, 'ok');
    }

    /**
     * 服务完成通知
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function completednotify(Request $request) {
        $snForCompany   = $request->input('app_order_sn');
        $order_sn       = $request->input('order_sn');
        $order = Order::where('sn_for_company', $snForCompany)->first();
        if ($order->finish_at == null) {
            $order->update([
                'finish_at' => $request->input('completed_at'),
                'status' => Order::STATUS_PHOTO_OK
            ]);
            //RongshuController::order_progress_notify($order->id);   // 通知荣数平台订单进度
            OrderService::notify_sip_use_status($order->id);
        }
        return self::success(['app_order_sn' => $snForCompany, 'order_sn' => $order_sn], 0, 'ok');
    }
}
